package com.cch.cooperation.common.web.session;

import com.cch.cooperation.common.web.util.RequestUtil;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

public class SessionManager {

    private BaseSessionPool sessionPool;

    public SessionManager(BaseSessionPool sessionPool) {
        this.sessionPool = sessionPool;
    }

    public void add(Session session) {
        sessionPool.add(session);
    }

    public void update(Session session) {
        sessionPool.update(session);
    }

    public void remove(String accessToken) {
        sessionPool.delete(accessToken);
    }

    public <T> Session<T> get(String accessToken) {
        return sessionPool.get(accessToken);
    }

    public <T> Session<T> get() {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = servletRequestAttributes.getRequest();
        return sessionPool.get(RequestUtil.getAccessToken(request));
    }

    public <T> Session<T> getById(String id) {
        return sessionPool.getById(id);
    }
}
